home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / src / sector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-10  |  3.0 KB  |  171 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  Peter's Final Project -- A texture mapping demonstration
  3.  *  © 1995, Peter Mattis
  4.  *
  5.  *  E-mail:
  6.  *  petm@soda.csua.berkeley.edu
  7.  *
  8.  *  Snail-mail:
  9.  *   Peter Mattis
  10.  *   557 Fort Laramie Dr.
  11.  *   Sunnyvale, CA 94087
  12.  *
  13.  *  Avaible from:
  14.  *  http://www.csua.berkeley.edu/~petm/final.html
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  */
  30.  
  31. #include <assert.h>
  32. #include "sector.h"
  33. #include "sys.stuff.h"
  34.  
  35. /*
  36.  * Keep a list of free sectors for fast allocation.
  37.  * Keep track of the memory used by sectors.
  38.  */
  39.  
  40. static SECTORS free_sectors_list = NULL;
  41. static long sector_mem = 0;
  42.  
  43. /*
  44.  * Make a sector object and initialize its values to something decent.
  45.  */
  46.  
  47. SECTOR
  48. make_sector ()
  49. {
  50.     SECTORS sectors;
  51.     SECTOR sector;
  52.  
  53.     if (free_sectors_list)
  54.     {
  55.         sectors = free_sectors_list;
  56.         sector = sectors_first (sectors);
  57.         free_sectors_list = sectors_rest (free_sectors_list);
  58.         free_list (sectors);
  59.     }
  60.     else
  61.     {
  62.         sector_mem += sizeof (_SECTOR);
  63.  
  64.         sector = (SECTOR) ALLOC (sizeof (_SECTOR));
  65.     }
  66.  
  67.     set_sector_val (sector, 0);
  68.     set_sector_faces (sector, NULL);
  69.     set_sector_objects (sector, NULL);
  70.     set_sector_neighbors (sector, NULL);
  71.  
  72.     return sector;
  73. }
  74.  
  75. /*
  76.  * Free a sector by adding it to the free list.
  77.  */
  78.  
  79. void
  80. free_sector (s)
  81.     SECTOR s;
  82. {
  83.     LIST temp;
  84.  
  85.     temp = make_list ();
  86.     set_list_datum (temp, s);
  87.     set_list_next (temp, free_sectors_list);
  88.     free_sectors_list = temp;
  89. }
  90.  
  91. /*
  92.  * Return the last sector in a list of sectors.
  93.  * (Actually, the list containing the last sector).
  94.  */
  95.  
  96. SECTORS
  97. sectors_last (sectors)
  98.     SECTORS sectors;
  99. {
  100.     if (sectors)
  101.     {
  102.         while (sectors_rest (sectors))
  103.             sectors = sectors_rest (sectors);
  104.  
  105.         return sectors;
  106.     }
  107.  
  108.     return NULL;
  109. }
  110.  
  111. /*
  112.  * Append a sector to a list of sectors.
  113.  */
  114.  
  115. SECTORS
  116. sectors_append_sector (set, s)
  117.     SECTORS set;
  118.     SECTOR s;
  119. {
  120.     LIST n;
  121.  
  122.     n = make_list ();
  123.     set_list_datum (n, s);
  124.  
  125.     return ((SECTORS) list_append_list ((LIST) set, n));
  126. }
  127.  
  128. /*
  129.  * Prepend a sector to a list of sectors.
  130.  */
  131.  
  132. SECTORS
  133. sectors_prepend_sector (set, s)
  134.     SECTORS set;
  135.     SECTOR s;
  136. {
  137.     LIST n;
  138.  
  139.     n = make_list ();
  140.     set_list_datum (n, s);
  141.  
  142.     return ((SECTORS) list_prepend_list ((LIST) set, n));
  143. }
  144.  
  145. /*
  146.  * Free a list of sectors.
  147.  */
  148.  
  149. void
  150. free_sectors (set)
  151.     SECTORS set;
  152. {
  153.     if (set == NULL)
  154.         return;
  155.  
  156.     free_sectors (sectors_rest (set));
  157.  
  158.     set_sectors_rest (set, free_sectors_list);
  159.     free_sectors_list = set;
  160. }
  161.  
  162. /*
  163.  * Return sector memory usage.
  164.  */
  165.  
  166. long
  167. sector_mem_usage ()
  168. {
  169.     return sector_mem;
  170. }
  171.